package com.jiutong.meipenggang.ui.activitys;

import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.StringUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by suxi on 2017/5/27.
 */

public class PwdFrogetActivity extends BaseActivity implements RegisterAndLoginBiz {

    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.iv_forget_pwd_phone)
    ImageView ivForgetPwdPhone;
    @Bind(R.id.et_forget_pwd_phone)
    EditText etForgetPwdPhone;
    @Bind(R.id.view_forget_pwd_phone)
    View viewForgetPwdPhone;
    @Bind(R.id.btn_forget_pwd_next)
    Button btnForgetPwdNext;

    private UserInfoPresent userInfoPresent;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_password_reset;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("找回密码");

        //EditText监听事件
        etChangeListener();

        userInfoPresent = new UserInfoPresent(this, this);
    }

    //EditText监听事件
    private void etChangeListener() {
        etForgetPwdPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    ivForgetPwdPhone.setBackgroundResource(R.mipmap.ic_phone_pre);
                    viewForgetPwdPhone.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivForgetPwdPhone.setBackgroundResource(R.mipmap.ic_phone);
                    viewForgetPwdPhone.setBackgroundResource(R.color.default_gray);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }


    @OnClick({R.id.rl_common_title_back, R.id.btn_forget_pwd_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.btn_forget_pwd_next:
                if (isRightPhone()) {
                    userInfoPresent.getAuthCodeByFoget();
                }
                break;
            default:
        }
    }

    /**
     * 验证手机号是否正确
     */
    private boolean isRightPhone() {
        if (TextUtils.isEmpty(etForgetPwdPhone.getText().toString().trim())) {
            toastShortShow("请输入手机号码");
            return false;
        }

        if (!StringUtil.isMobileNumber(etForgetPwdPhone.getText().toString().trim())) {
            toastShortShow("手机号码格式有误，请输入正确手机号码");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return etForgetPwdPhone.getText().toString().trim();
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {
        if (subjects.getError()==-1){
            toastShortShow(subjects.getMsg());
            Bundle bundle = new Bundle();
            bundle.putString("phone",getPhoneNum());
            openActivity(VerifyCodeActivity.class,bundle);
            finish();
        }else{
            toastShortShow(subjects.getMsg());
        }
    }

    @Override
    public void onSuccess(Object object) {


    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 5;
    }


}
